package com.sleepcamel.rest.domain.filegenerator;

import java.io.File;
import java.io.IOException;
import java.util.List;

import com.sleepcamel.rest.domain.Mapping;
import com.sleepcamel.rest.domain.Project;
import com.sleepcamel.rest.domain.ProjectPackage;
import com.sleepcamel.rest.domain.util.RestConstants;

public class ProjectFilesGenerator implements IProjectFileGenerator {

	private Project project;
	private File projectDirectory;

	public ProjectFilesGenerator(Project project, File directory) {
		this.project = project;
		projectDirectory = directory;
	}
	
	public void generate() throws IOException{
		generateServerXML();
		generateMappingFiles(projectDirectory);
	}

	private void generateServerXML() {
	}

	private void generateMappingFiles(File projectDirectory) throws IOException {
		File serviceProjectDirectory = new File(projectDirectory, RestConstants.getServiceProjectName(project.getName()));
		File sourcesDirectory = new File(serviceProjectDirectory, RestConstants.JAVA_SOURCE_PATH);
		sourcesDirectory.mkdirs();

		MappingFileGenerator mappingGenerator = new MappingFileGenerator(sourcesDirectory);
		List<ProjectPackage> projectPackages = project.getChilds();
		for(ProjectPackage projectPackage:projectPackages){
			for(Mapping mapping:projectPackage.getAllPackageMappings()){
				mappingGenerator.setMapping(mapping);
				mappingGenerator.generate();
			}
		}
	}

}
